﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using spUtils;
using spData.Common;
using spMapping;

namespace spWeb.Page.HT
{
    public partial class ht_Zbsp : TaskPage
    {
        #region Property

        #endregion

        #region Method Init Controls Value & Show/Hide & Enable/Disable & Opinion
        private void InitControls()
        {
            #region Init controls value
            Expression exp = new Expression();
            string did = Request.QueryString["DID"];
            exp = string.IsNullOrEmpty(did) ? dc_ht_Zbsp.process == TaskAPI.Process & dc_ht_Zbsp.incident == TaskAPI.Incident : dc_ht_Zbsp.id == did;
            if (TaskAPI.IsInitiate)
            {
                exp &= dc_ht_Zbsp.userName == USER.UserName;
            }

            dc_ht_Zbsp v = exp.ListFirst<dc_ht_Zbsp>();
            if (v != null)
            {
                SetControlsValue(v);
                //DID方式加载
                if (!string.IsNullOrEmpty(did))
                {
                    TaskAPI.LoadByDID(v.Process, v.Incident);
                    Process = v.Process;
                    Incident = v.Incident;
                }
            }
            else
            {
                UserFullName.Text = USER.UserFullName;
                Time.Text = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm");
                Department.Text = USER.Department;
            }

            //加载招标方案链接
            BidSchemeCode_TextChanged(ViewID, null);

            #endregion

            #region Init Show/Hide、Enable/Disable
            bool flg = TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit;
            //SetVisible(flg, R_Subject,R_BidSchemeCode,R_BidGroupMembers,R_PredictPassedBidder,R_RecommandSuccessBidder);
            //SetEnable(flg, Subject,pl_zbxx, pl_dbxx);
            #endregion
        }
        #endregion

        #region Method Data Validation
        private List<string> DataValidation()
        {
            List<Regular> regs = new List<Regular>();
            if (!AssExt.IsConferWithBack && !AssExt.IsForwardWithBack)
            {
                if (TaskAPI.IsBeginStep)
                {
                    regs.Add(new Regular(RegularExp.NotEmpty, Subject.Text, "事项主题为必填项!"));
                    regs.Add(new Regular(RegularExp.NotEmpty, BidSchemeCode.Text, "招标方案编号为必填项!"));
                    regs.Add(new Regular(RegularExp.NotEmpty, BidType.Text, "招标类型为必填项!"));
                    regs.Add(new Regular(RegularExp.NotEmpty, "","招标小组成员为必填项!"));
                    regs.Add(new Regular(RegularExp.NotEmpty, PredictPassedBidder.Text, "招标方案金额总值为必填项!"));
                    regs.Add(new Regular(RegularExp.NotEmpty, PublishDate.Text, "发布时间为必填项!"));
                    regs.Add(new Regular(RegularExp.NotEmpty, PublishWay.Text, "发布地点为必填项!"));
                    regs.Add(new Regular(RegularExp.NotEmpty, TenderDate.Text, "开标时间为必填项!"));
                    regs.Add(new Regular(RegularExp.NotEmpty, TenderAddress.Text, "开标地点为必填项!"));
                    regs.Add(new Regular(RegularExp.NotEmpty, RecommandSuccessBidder.Text, "推荐中标人为必填项!"));
                    regs.Add(new Regular(RegularExp.NotEmpty, RecommandSuccessBidderUnitPirce.Text, "推荐中标金额单位值为必填项!"));
                    regs.Add(new Regular(RegularExp.NotEmpty, RecommandSuccessBidderTotalPirce.Text, "推荐中标金额总值为必填项!"));
                    regs.Add(new Regular(RegularExp.Float, RecommandSuccessBidderUnitPirce.Text, "推荐中标金额单位值为数值类型!"));
                    regs.Add(new Regular(RegularExp.Float, RecommandSuccessBidderTotalPirce.Text, "推荐中标金额总值为数值类型!"));
                }
            }
            return Validation(regs);
        }
        #endregion


        #region Method Engine Transfer Value
        private void EngineTransferValue()
        {
            if (TaskAPI.IsBeginStep)
            {
                TaskAPI.Summary = Subject.Text;
            }
            TaskAPI["e_Approve"] = AssExt.Assessment.Opinion;
        }

        #endregion

        #region Method Submit
        private bool Submit(bool IsSend)
        {
            #region “开始”步骤
            //if (TaskAPI.IsBeginStep)
            {
                dc_ht_Zbsp v = (dc_ht_Zbsp.process == TaskAPI.Process & dc_ht_Zbsp.incident == TaskAPI.Incident
                                & dc_ht_Zbsp.userName == (TaskAPI.IsInitiate ? USER.UserName : TaskAPI.UserName)).ListFirst<dc_ht_Zbsp>();
                bool result = GetControlsValue<dc_ht_Zbsp>(ref v);
                if (result)
                {
                    v.Process = TaskAPI.Process;
                    v.Incident = TaskAPI.Incident;
                    v.UserName = USER.UserName;
                    v.UserFullName = USER.UserFullName;
                    v.Time = System.DateTime.Now;
                }
                ORM.Save(v);

            }
            #endregion


            if (IsSend)
            {
                EngineTransferValue();
                ORM.Save(AssExt.Assessment);
                TaskAPI.Send();
            }

            if (IsSend && TaskAPI.IsInitiate)
            {
                (dc_ht_Zbsp.process == TaskAPI.Process & dc_ht_Zbsp.incident == 0 & dc_ht_Zbsp.userName == USER.UserName).Edit<dc_ht_Zbsp>(p => p.Incident = TaskAPI.Incident);
            }

            //退出
            if (IsSend)
            {
                Exit();
            }
            return true;
        }
        #endregion

        #region Event Page
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitControls();
            }
        }
        #endregion

        #region Event TextChanged

        protected void RecommandSuccessBidderTotalPirce_TextChanged(object sender, EventArgs e)
        {
            RecommandSuccessBidderTotalPirceCN.Text = Utils.TransformNumberToChina(RecommandSuccessBidderTotalPirce.Text.Replace(",", ""));
        }
        #endregion

        #region Event Button Click

        #region Event Save As Draft
        protected void SaveAsDraft_Click(object sender, EventArgs e)
        {
            bool success = Submit(false);
            Anthem.Manager.AddScriptForClientSideEval(string.Format("JSInfomation('{0}')", success ? "保存成功!" : "保存失败!"));
        }
        #endregion

        #region Event Send
        protected void Send_Click(object sender, EventArgs e)
        {
            List<string> msg = DataValidation();
            if (msg.Count > 0)
            {
                Anthem.Manager.AddScriptForClientSideEval(Alert(msg));
                return;
            }

            bool success = Submit(true);
            (sender as spWeb.Controls.AssessmentExt).Refresh();
            Anthem.Manager.AddScriptForClientSideEval(string.Format("JSInfomation('{0}');{1}", success ? "提交成功!" : "提交失败!", success ? "window.close();" : ""));
        }
        #endregion

        #region Event Aborted
        protected void Aborted_Click(object sender, EventArgs e)
        {
            (dc_ht_Zbsp.process == TaskAPI.Process & dc_ht_Zbsp.incident == TaskAPI.Incident).Edit<dc_ht_Zbsp>(p => { p.Status = 4; });
        }
        #endregion

        #region 招标方案编号 回传事件
        protected void BidSchemeCode_TextChanged(object sender, EventArgs e)
        {
            string guid = BidSchemeCode.Text;
            //页面初次加载的时候处理
            if (sender is Label)
            {
                guid = ((Label)sender).Text.Trim();
            }
           
            dc_ht_Zbfasp v = (dc_ht_Zbfasp.id == guid).ListFirst<dc_ht_Zbfasp>();
            if (v != null)
            {
                BidSchemeCode.Text = v.BidTitle;
                if (v.BidType != null)
                {
                    BidType.Text = v.BidType.Replace("<+>","");
                }
                BidGroupMembers.Text = v.BidGroupMembers;
                ViewID.Text = guid;
                View.Visible = true;
                View.Text = GetView(v.ID);
            }
        }

        private string GetView(string id)
        {
            return string.Format("<a href='{0}?DID={1}' target='blank'>查看</a>", ResolveUrl("~/Page/HT/ht_Zbfasp.aspx"), id);
        }

        #endregion        

        #endregion
    }
}